﻿#include <iostream>
#include <list>
#include <string>
#include <Windows.h>
#include "../CVDance/detection.h"


std::list<std::string> GetAllFilesInDirectory(const std::string& directoryPath) {
	std::list<std::string> fileList;

	// 构造搜索路径（确保以 \* 结尾）
	std::string searchPath = directoryPath;
	if (searchPath.back() != '\\' && searchPath.back() != '/') {
		searchPath += '\\';
	}
	searchPath += "*";

	// 开始文件搜索
	WIN32_FIND_DATAA findData;
	HANDLE hFind = FindFirstFileA(searchPath.c_str(), &findData);

	if (hFind == INVALID_HANDLE_VALUE) {
		DWORD error = GetLastError();
		if (error != ERROR_FILE_NOT_FOUND) {
			std::cerr << "Error opening directory: " << directoryPath
				<< " (Error code: " << error << ")" << std::endl;
		}
		return fileList;
	}

	do {
		// 跳过目录（包括 "." 和 ".."）
		if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
			continue;
		}
		// 将文件名加入列表
		fileList.push_back(findData.cFileName);
	} while (FindNextFileA(hFind, &findData));

	FindClose(hFind);
	return fileList;
}

int main()
{

	if (!InitDetection()) {
		std::cerr << "初始化检测库失败！" << std::endl;
		return -1;
	}

	std::string targetDirectory;

	std::cout << "请输入完整图片文件夹路径" << std::endl;
	std::cin >> targetDirectory;

	// 获取文件列表
	std::list<std::string> files = GetAllFilesInDirectory(targetDirectory);

	// 遍历输出文件名
	std::cout << "Files in directory " << targetDirectory << ":\n";
	for (const auto& fileName : files) {

		DetectionResult* results = nullptr;

		std::string fullPath = targetDirectory;
		if (!fullPath.empty() && fullPath.back() != '\\') {
			fullPath += '\\';
		}
		fullPath += fileName;

		int count = DetectImage(fullPath.c_str(), &results);

		if (count < 0) {
			std::cerr << "检测失败，错误码：" << count << std::endl;
			ReleaseDetection();
			return -1;
		}

		std::cout << "检测到 " << count << " 个对象：" << std::endl;
		for (int i = 0; i < count; i++) {
			auto& r = results[i];
			std::cout << "类别: " << r.class_name
				<< ", 置信度: " << r.confidence
				<< ", 坐标: [" << r.x << "," << r.y << "," << r.width << "," << r.height << "]" << std::endl;
		}

		FreeResults(results);

	}

    
	ReleaseDetection();

    return 0;
}

